Skip to content

Conversation

@aldevv
Copy link
Contributor

@aldevv aldevv commented Oct 2, 2025

Summary by CodeRabbit

  • Improvements

    • Added user and team caching across syncs, speeding up operations and reducing API calls for orgs, teams, repos, roles, invitations, and users.
    • More consistent user/team lookups improve reliability of grants and revocations.
  • Bug Fixes

    • Rate-limit errors now include limit/remaining/reset details for clearer troubleshooting.
    • Listing, grant, revoke, and related flows handle rate-limiting more gracefully, reducing intermittent failures.

@coderabbitai
Copy link

coderabbitai bot commented Oct 2, 2025

Walkthrough

Adds internal user and team caches, wires them through builders and resource types, replaces direct GitHub lookups with cache-backed access, and standardizes rate-limit error wrapping to include response metadata. Initializes caches in connector setup and updates related syncers and builders accordingly.

Changes

Cohort / File(s) Summary of edits
Caching primitives
pkg/connector/helpers.go
Added unexported userDataCache and teamDataCache with thread-safe maps, double-checked locking, API fetch on cache miss, and newUserDataCache / newTeamDataCache constructors.
Connector initialization & syncers
pkg/connector/connector.go
Added userCache and teamCache fields on GitHub; initialize caches in New; pass caches into builders and ResourceSyncers; switched some rate-limit error wrapping to WrapErrorsWithRateLimitInfo.
Org & Org role resources
pkg/connector/org.go, pkg/connector/org_role.go
Added userCache fields; replaced direct API user lookups with cache methods; updated builders to accept userCache; standardized rate-limit error wrapping to include response info.
Team resources
pkg/connector/team.go
Added userCache and teamCache; replaced team/user API calls with cache-backed lookups; updated teamBuilder signature; standardized rate-limit error wrapping.
Repository resources
pkg/connector/repository.go
Added userCache and teamCache; replaced user/team lookups with cache methods; updated repositoryBuilder signature; standardized rate-limit error wrapping.
User resources
pkg/connector/user.go
Added userCache to userResourceType; replaced direct user API calls with cache lookups; updated userBuilder signature and call sites; standardized rate-limit error wrapping and adjusted 404 fallback handling.
Enterprise roles
pkg/connector/enterprise_role.go
Added userCache to type and builder; replaced direct user fetch with cache-backed lookup; switched rate-limit error wrapping to include response metadata.
Invitations
pkg/connector/invitation.go
Plumbed userCache through invitationBuilderParams and invitationResourceType; no other logic changes beyond wiring.
Token API rate-limit handling
pkg/connector/api_token.go
Replaced WrapErrors with WrapErrorsWithRateLimitInfo in List error handling to include response rate-limit details.
Tests updated for new builder signatures
pkg/connector/*_test.go (org_role_test.go, org_test.go, repository_test.go, team_test.go, user_test.go)
Updated tests to construct newUserDataCache and newTeamDataCache where required and to pass additional cache parameters into builders.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant R as Resource (Repo/Org/Team/User)
  participant UC as UserCache
  participant TC as TeamCache
  participant GH as GitHub API
  participant EH as Error Wrapper

  R->>UC: GetUser(id/login)
  alt Cache hit
    UC-->>R: User
  else Cache miss
    UC->>GH: Fetch User
    alt Success
      GH-->>UC: User + Resp
      UC-->>R: User (cached)
    else Rate-limited / error
      GH-->>UC: Error + Resp
      UC->>EH: WrapErrorsWithRateLimitInfo(resp, err)
      EH-->>R: Unavailable error with rate-limit info
    end
  end

  Note over R,TC: GetTeam(id) follows analogous flow via TeamCache
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Poem

I cached a carrot, crisp and bright,
So future hops would take less byte;
When limits bark and gates say “wait,”
I read their pulses, mark their state.
With users, teams snug in my lair,
I bound through fields—fast, light, and fair. 🥕🐇

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.56% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly describes the primary change of introducing caches to reduce API calls and maps directly to the changeset's focus on adding caching layers across connectors.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch improve_api_calls

Comment @coderabbitai help to get the list of available commands and usage tips.

@aldevv aldevv force-pushed the improve_api_calls branch from c87b79a to 0be9a64 Compare October 2, 2025 16:10
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
pkg/connector/helpers.go (1)

141-177: Address the TODO for deprecated API usage.

The teamDataCache implementation is correct, but the TODO comment at line 162 indicates that GetTeamByID is deprecated and should be migrated to GetTeamBySlug.

Do you want me to track this migration task by opening a new issue, or would you prefer to address it in this PR?

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c87b79a and fb8894d.

📒 Files selected for processing (14)
  • pkg/connector/api_token.go (1 hunks)
  • pkg/connector/connector.go (4 hunks)
  • pkg/connector/enterprise_role.go (3 hunks)
  • pkg/connector/helpers.go (1 hunks)
  • pkg/connector/org.go (8 hunks)
  • pkg/connector/org_role.go (6 hunks)
  • pkg/connector/org_role_test.go (2 hunks)
  • pkg/connector/org_test.go (1 hunks)
  • pkg/connector/repository.go (8 hunks)
  • pkg/connector/repository_test.go (1 hunks)
  • pkg/connector/team.go (8 hunks)
  • pkg/connector/team_test.go (1 hunks)
  • pkg/connector/user.go (5 hunks)
  • pkg/connector/user_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/connector/user.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: ci
🔇 Additional comments (39)
pkg/connector/api_token.go (1)

98-98: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, aligning with the broader PR pattern for standardizing rate-limit error handling.

pkg/connector/helpers.go (1)

70-139: LGTM!

The userDataCache implementation is correct:

  • Double-checked locking is properly implemented in both GetUser and GetUserByLogin.
  • Cache coherence is maintained by populating both usersById and usersByLogin maps on cache miss.
  • Error handling and early return on cache hit ensure minimal lock contention.
pkg/connector/user_test.go (1)

47-54: LGTM!

The test correctly constructs and injects the userCache into userBuilder, aligning with the broader PR pattern of cache-backed resource access.

pkg/connector/org_test.go (1)

26-27: LGTM!

The test correctly constructs and injects the userCache into orgBuilder, aligning with the broader PR pattern of cache-backed resource access.

pkg/connector/org_role_test.go (2)

27-28: LGTM!

The test correctly constructs and injects the userCache into orgRoleBuilder, aligning with the broader PR pattern of cache-backed resource access.


93-94: LGTM!

The second test case correctly constructs and injects the userCache into orgRoleBuilder, consistent with the first test case and the broader PR pattern.

pkg/connector/team_test.go (1)

27-29: LGTM!

The test correctly constructs and injects both userCache and teamCache into teamBuilder, aligning with the broader PR pattern of cache-backed resource access.

pkg/connector/org.go (7)

41-41: LGTM!

The addition of the userCache field to orgResourceType correctly wires the user data cache through the type, enabling cache-backed user lookups.


107-107: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, aligning with the broader PR pattern.


135-135: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, consistent with the broader PR pattern.


233-233: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, consistent with the broader PR pattern.


298-298: LGTM!

Replacing the direct GetByID call with userCache.GetUser correctly leverages the cache to reduce API calls, aligning with the PR objective.


390-390: LGTM!

Replacing the direct GetByID call with userCache.GetUser correctly leverages the cache to reduce API calls, consistent with the change at line 298.


420-436: LGTM!

The orgBuilder correctly accepts and stores the userCache, ensuring orgResourceType has access to cached user data for Grant and Revoke operations.

pkg/connector/team.go (9)

63-64: LGTM!

The addition of userCache and teamCache fields to teamResourceType correctly wires the caches through the type, enabling cache-backed lookups.


101-101: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, aligning with the broader PR pattern.


112-118: LGTM!

Replacing the direct GetTeamByID call with teamCache.GetTeam correctly leverages the cache to reduce API calls, aligning with the PR objective.


115-115: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, consistent with the broader PR pattern.


178-178: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, consistent with the broader PR pattern.


217-217: LGTM!

The update to WrapErrorsWithRateLimitInfo adds rate-limit metadata to the unavailable error response, consistent with the broader PR pattern.


307-307: LGTM!

Replacing the direct GetByID call with userCache.GetUser correctly leverages the cache to reduce API calls, aligning with the PR objective.


367-367: LGTM!

Replacing the direct GetByID call with userCache.GetUser correctly leverages the cache to reduce API calls, consistent with the change at line 307.


379-387: LGTM!

The teamBuilder correctly accepts and stores both userCache and teamCache, ensuring teamResourceType has access to cached data for all team-related operations.

pkg/connector/repository_test.go (1)

27-29: LGTM!

The test correctly instantiates the new userCache and teamCache and passes them to repositoryBuilder, aligning with the updated constructor signature.

pkg/connector/connector.go (4)

100-101: LGTM!

The new userCache and teamCache fields in the GitHub struct provide a clean way to store and pass cache instances through the connector's lifecycle.


108-126: LGTM!

All resource syncers are correctly wired with the new userCache and teamCache instances, enabling cache-backed lookups across the board.


320-321: LGTM!

The caches are initialized cleanly alongside the existing orgCache, ensuring they're ready for use when builders are constructed.


462-462: LGTM!

Updating the error wrapping to WrapErrorsWithRateLimitInfo standardizes rate-limit error reporting and includes useful response metadata for observability.

pkg/connector/org_role.go (4)

43-43: LGTM!

Adding userCache to orgRoleResourceType follows the consistent pattern across all resource types in this PR.


95-95: LGTM!

The updated error wrapping consistently includes rate-limit information from the response, improving observability and debugging.

Also applies to: 232-232


329-329: LGTM!

Replacing direct GitHub client user lookups with o.userCache.GetUser centralizes user retrieval and reduces redundant API calls.

Also applies to: 404-404


427-433: LGTM!

The updated orgRoleBuilder signature correctly accepts and wires userCache into the resource type.

pkg/connector/repository.go (4)

63-64: LGTM!

Adding userCache and teamCache to repositoryResourceType enables cache-backed retrieval of users and teams, reducing redundant API calls.


187-187: LGTM!

The updated error wrapping consistently propagates rate-limit details from the response, aligning with the PR's goal of improved observability.

Also applies to: 243-243


324-324: LGTM!

Replacing direct client calls with userCache.GetUser and teamCache.GetTeam centralizes retrieval logic and reduces API traffic.

Also applies to: 341-341, 389-389, 399-399


420-427: LGTM!

The updated repositoryBuilder signature correctly accepts and wires both userCache and teamCache into the resource type.

pkg/connector/enterprise_role.go (3)

25-25: LGTM!

Adding userCache to enterpriseRoleResourceType follows the consistent pattern across all resource types in this PR.


143-143: LGTM!

Replacing direct GitHub client lookup with o.userCache.GetUserByLogin and updating error wrapping to include rate-limit information are both consistent improvements.

Also applies to: 146-146


166-175: LGTM!

The updated enterpriseRoleBuilder signature correctly accepts and wires userCache into the resource type.

@aldevv aldevv requested a review from a team October 16, 2025 15:40
Copy link
Contributor

@agustin-conductor agustin-conductor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@agustin-conductor agustin-conductor requested a review from a team October 16, 2025 19:49
Copy link
Contributor

@btipling btipling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to wait for the new baton-sdk caching changes to land before merging this.

@aldevv aldevv closed this Oct 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants